<#include "../base.ftl"/>
<#macro css>

</#macro>

<#macro js>

</#macro>

<#macro title>
广大物流云平台
</#macro>
<#macro script>
     var setting = {
            async: {
                enable: true,
                url: '${request.contextPath}/admin/shipTree.action',
                datatype : 'json',
                autoParam:["id","name","pId"]
            },
            data: {
                simpleData: {
                       enable: true
                }
            },
            callback:{
                onNodeCreated : zTreeonNodeCreated,
                onClick:zTreeClick
            }
            
        };
  function fillCategoryId(params,postdata){
      var treeObj=$.fn.zTree.getZTreeObj("shipTree");
      var nodes=treeObj.getSelectedNodes();
      var add_data=new Object();
      var a=nodes[0].id;
      //alert(a);
      a=a.replace("B","");
      add_data["shippingId"]=a;
      add_data["beanName"]="tsm.TsmVoyage";
      return add_data;
  }
    function zTreeClick(event,treeId,treeNode){
    
         $.ajax({
            url: '${request.contextPath}/operator.action',
            type:'post',
            data:{'shippingId':shippingId,'voyageCode':code,"oper":"edit","beanName":"tsm.TsmVoyage",'voyageRoute':route,"id":vogeId},
            dataType : 'json',
            success:function(data){
               data.flag
               if(true){
                
               jQuery("#gridTable").trigger('reloadGrid');
               }
            }
        });
    
    
    
    
    
    
    
    
    
        var a=treeNode.id;
          a=a.replace("B","");
        //alert(a);
            jQuery("#gridTable").jqGrid('setGridParam',{
                url : "${request.contextPath}/list!list.action?shippingId="+a,
                page : 1 
            });
            jQuery("#gridTable").trigger('reloadGrid');
        }
        
     function zTreeonNodeCreated(event,treeId,treeNode){
        var treeObj=$.fn.zTree.getZTreeObj(treeId);
        var list=treeObj.getNodeByParam("level",'0');
        treeObj.expandNode(list,true,true,true);
     
     
     }

        jQuery(document).ready(function(){
            $.fn.zTree.init($("#shipTree"), setting);
        });

$(function(){
    $("#gridTable").jqGrid({
    //list表格获取地址
        //list表格数据获取地址
        url:'${request.contextPath}/list!list.action',
        //list表格编辑地址
        editurl :'${request.contextPath}/operator.action',
        datatype: "json",
        mtype:"POST",
        height: 200,
        width: 1000,
        colNames:['船舶ID','航次编码','航次代码','航线','起航标志','当前航次标志','状态','创建人','创建日期','最后修改人','最后修改时间'],
        colModel:[
            {name:'shippingId',jsonmap:'SHIPPING_ID',index:'SHIPPING_ID',editable:false,hidden:true},          
            {name:'voyageId',jsonmap:'VOYAGE_ID',index:'VOYAGE_ID',hidden:true,editable:false},     
            {name:'voyageCode',jsonmap:'VOYAGE_CODE',editable:true,index:'VOYAGE_CODE'},      
            {name:'voyageRoute',jsonmap:'VOYAGE_ROUTE',editable:true,index:'VOYAGE_ROUTE'},
            {name:'setSailFlag',jsonmap:'SETSAIL_FLAG',editable:false,index:'SETSAIL_FLAG',hidden:true},
            {name:'currentFlag',jsonmap:'CURRENT_FLAG',index:'CURRENT_FLAG',editable:false,hidden:true},
            {name:'status',jsonmap:'STATUS',index:'STATUS',editable:false,hidden:true,
              
            },
            {name:'createdBy',jsonmap:'CREATED_BY',editable:false,hidden:true},
            {name:'createdDate',jsonmap:'CREATED_DATE',editable:false,hidden:true,
                editoptions :  {dataInit: function(element) {
                    $(element).datepicker({changeMonth : true,changeYear : true,dateFormat : 'yy-mm-dd'});
                }}       
            },
            {name:'lastUpdBy',jsonmap:'LAST_UPD_BY',editable:false,hidden:true},
            {name:'lastUpdDate',jsonmap:'LAST_UPD_DATE',editable:false,hidden:true,
              editoptions :  {dataInit: function(element) {
                    $(element).datepicker({changeMonth : true,changeYear : true,dateFormat : 'yy-mm-dd'});
                }}  
            }     
        ],
        sortname:'VOYAGE_ID',
        sortorder:'ASC',
        viewrecords : true,
        gridview : true,
        rownumbers : true,
        rowNum : 20,
        rowList : [10,20,30],
        //onSelectRow:cellSelect,
        jsonReader : {
            page : "page.pageNo",
            total : "page.totalPages",
            records : "page.totalCount",
            root : "page.result",
            repeatitems : false,
            id : 'VOYAGE_ID'
        },
        
        prmNames : {
            rows : "page.pageSize",
            page : "page.pageNo",
            sort : "page.orderBy",
            order : "page.order"
        },
        loadBeforeSend : function(xhr,settings){
            //设置访问的list标签
            settings.data=settings.data+ "&gridKey=/list#TsmVoyage";
            return true;
        },
        pager : "#gridPager",
        caption : "航次列表"
    }).navGrid('#gridPager',{
            //edit:true,
            editfunc:cellSelect,
            add:true, 
            //addfunc:cellSelect,
            //del:true, 
            delfunc:selectDel,
            search: false
    },{ 
        //edit
        //beforeSubmit:cellSelect,
        reloadAfterSubmit:true,
        closeAfterEdit:true,//新增完毕后关闭
        afterComplete:opertaionPrompt,//更新结束后提示信息
        //onclickSubmit: fillCategoryId  //触发的事件
        
        
        
    },{
    //add
        reloadAfterSubmit : true,
        closeAfterAdd : true, //新增完毕后关闭
        afterComplete : opertaionPrompt,
        onclickSubmit: fillCategoryId
        
    
    },{
      //del
        reloadAfterSubmit : true, 
        closeAfterEdit : true,  //修改完毕后关闭
        afterComplete : opertaionPrompt, //更新结束后提示信息
        //onclickSubmit: fillCategoryId,    
        
    })
    
});

 var cellSelect=function(){
    var selectedId = $("#gridTable").jqGrid(
            "getGridParam", "selrow");
    var setsailFlag=jQuery("#gridTable").jqGrid('getRowData',selectedId).setSailFlag;
          //alert(setsailFlag);
    var voyageCode=jQuery("#gridTable").jqGrid('getRowData',selectedId).voyageCode;
    var voyageRoute=jQuery("#gridTable").jqGrid('getRowData',selectedId).voyageRoute;
   //alert(status);
   var reg=/^1$/;
   var isMatch=reg.test(setsailFlag);
   //alert(isMatch);
   if(isMatch){
     alert("已起航，不可编辑");
   }else{
     var consoleDlg = $("#gridDialog");   
     var dialogButtonPanel = consoleDlg.siblings(".ui-dialog-buttonpane"); 
     consoleDlg.find("#code").removeAttr().val(voyageCode);
      consoleDlg.find("#route").removeAttr().val(voyageRoute);
     consoleDlg.dialog("option", "title", "修改记录").dialog("open"); 
   }
 }
 $("#gridDialog").dialog({  
         autoOpen: false,      
         modal: true,    // 设置对话框为模态（modal）对话框  
         resizable: true,      
         width: 480,  
         open : function(){
            //var selectedId=$("#dialogId").jqGrid("getGridParm","selrow");
            $("#gridTable").jqGrid({
                url: '${request.contextPath}/list!list.action',
                datatype: "json",
                mtype:"POST",
                 colNames:['船舶ID','航次编码','航次代码','航线','起航标志','当前航次标志','状态','创建人','创建日期','最后修改人','最后修改时间'],
        colModel:[
            {name:'shippingId',jsonmap:'SHIPPING_ID',index:'SHIPPING_ID',editable:false,hidden:true},          
            {name:'voyageId',jsonmap:'VOYAGE_ID',index:'VOYAGE_ID',hidden:true,editable:false},     
            {name:'voyageCode',jsonmap:'VOYAGE_CODE',editable:true,index:'VOYAGE_CODE'},      
            {name:'voyageRoute',jsonmap:'VOYAGE_ROUTE',editable:true,index:'VOYAGE_ROUTE'},
            {name:'setSailFlag',jsonmap:'SETSAIL_FLAG',editable:false,index:'SETSAIL_FLAG',hidden:true},
            {name:'currentFlag',jsonmap:'CURRENT_FLAG',index:'CURRENT_FLAG',editable:false,hidden:true},
            {name:'status',jsonmap:'STATUS',index:'STATUS',editable:false,hidden:true,
              
            },
            {name:'createdBy',jsonmap:'CREATED_BY',editable:false,hidden:true},
            {name:'createdDate',jsonmap:'CREATED_DATE',editable:false,hidden:true,
                editoptions :  {dataInit: function(element) {
                    $(element).datepicker({changeMonth : true,changeYear : true,dateFormat : 'yy-mm-dd'});
                }}       
            },
            {name:'lastUpdBy',jsonmap:'LAST_UPD_BY',editable:false,hidden:true},
            {name:'lastUpdDate',jsonmap:'LAST_UPD_DATE',editable:false,hidden:true,
              editoptions :  {dataInit: function(element) {
                    $(element).datepicker({changeMonth : true,changeYear : true,dateFormat : 'yy-mm-dd'});
                }}  
            }     
        ],
        sortname:'VOYAGE_ID',
        sortorder:'ASC',
        viewrecords : true,
        gridview : true,
        rownumbers : true,
        rowNum : 20,
        rowList : [10,20,30],
        //onSelectRow:cellSelect,
        jsonReader : {
            page : "page.pageNo",
            total : "page.totalPages",
            records : "page.totalCount",
            root : "page.result",
            repeatitems : false,
            id : 'VOYAGE_ID'
        },
        
        prmNames : {
            rows : "page.pageSize",
            page : "page.pageNo",
            sort : "page.orderBy",
            order : "page.order"
        },
        loadBeforeSend : function(xhr,settings){
            //设置访问的list标签
            settings.data=settings.data+ "&gridKey=/list#TsmVoyage";
            return true;
        },
        pager : "#gridPager",
        caption : "修改航次信息"
    });
     jQuery("#selectId").trigger('reloadGrid');
     },
     buttons: {//为对话框添加按钮
         "确定":addUser,
         "取消":function() {$("#gridDialog").dialog("close")}
       }
     
});
 function addUser(){
      var gd =$("#gridDialog");
      var code=$.trim(gd.find("#code").val());
      //alert(code);
      var route=$.trim(gd.find("#route").val());
      //alert(route);
      var selectedId = $("#gridTable").jqGrid("getGridParam", "selrow")
            var vogeId=jQuery("#gridTable").jqGrid('getRowData',selectedId).voyageId;
            //alert(vogeId);
    var shippingId=jQuery("#gridTable").jqGrid('getRowData',selectedId).shippingId;
    $.ajax({
            url: '${request.contextPath}/operator.action',
            type:'post',
            data:{'shippingId':shippingId,'voyageCode':code,"oper":"edit","beanName":"tsm.TsmVoyage",'voyageRoute':route,"id":vogeId},
            dataType : 'json',
            success:function(result){
                alert(result.message);
                jQuery("#gridTable").trigger('reloadGrid');      
                $("#gridDialog").dialog("close")
            }
        });
      
 }
 
 var selectDel=function(){
    var selectedId = $("#gridTable").jqGrid(
            "getGridParam", "selrow");
    //var voyageId=jQuery("#gridTable").jqGrid('getRowData',selectedId).voyageId;
    var setsailFlag=jQuery("#gridTable").jqGrid('getRowData',selectedId).setSailFlag;
    
      //  alert("voyageId:"+voyageId);
   //alert(status);
   var reg=/^1$/;
   var isMatch=reg.test(setsailFlag);
   //alert(isMatch);
   if(isMatch){
     alert("已起航，不可删除");
     }else{
      var consoleDlg = $("#delDialog");   
      var dialogButtonPanel = consoleDlg.siblings(".ui-dialog-buttonpane"); 
      consoleDlg.dialog("option", "title", "确定删除记录？").dialog("open"); 

   }
 }
 
 $("#delDialog").dialog({  
         autoOpen: false,      
         modal: true,    // 设置对话框为模态（modal）对话框  
         resizable: true,      
         width: 480,  
         buttons: {//为对话框添加按钮
         "确定":delUser,
         "取消":function() {$("#gridDialog").dialog("close")}
       }
     
});
 function delUser(){
      var selectedId = $("#gridTable").jqGrid(
            "getGridParam", "selrow");
      //alert(selectedId);
      //var voyageId=jQuery("#gridTable").jqGrid('getRowData',selectedId).voyageId;
    //var shippingId=jQuery("#gridTable").jqGrid('getRowData',selectedId).shippingId;
    $.ajax({
            url: '${request.contextPath}/operator.action',
            type:'post',
            data:{"oper":"del","beanName":"tsm.TsmVoyage","id":selectedId},
            dataType : 'json',
            success:function(request){
            alert(request.message);
                jQuery("#gridTable").trigger('reloadGrid');      
                $("#delDialog").dialog("close")
            }
        });
      
 }

</#macro>
<#macro main>
  <div id="treediv" class="ui-widget-content ui-corner-all" style="float:left;width:200px">
           <ul id="shipTree" class="ztree"></ul>
  </div>
  <div id="detaildiv" style="float:left;width:200px">
           <table id="gridTable"></table>
           <div id="gridPager"></div>
  </div>
  <div id="gridDialog">  
                <form id="consoleForm">  
                    <input type="hidden" id="selectId" />  
                    <table class="formTable" id="dialogId">  
                        <tr>  
                            <th><span class="ui-widget-header">航次代码</span></th>  
                            <td>  
                                <input type="text" class="textField" id="code" name="code" />  
                            </td>  
                            
                        </tr>
                        <tr>
                        <th><span class="ui-widget-header">航线</span></th>  
                            <td>  
                                <input type="text" class="textField" id="route" name="route" />  
                            </td>   
                        </tr>     
                    </table>  
                </form>  
            </div>  
    <div id="delDialog">
      
    
    </div>
</#macro>